import React, { Component, Fragment } from 'react';
import { View, Text, StatusBar ,SafeAreaView,PermissionsAndroid} from 'react-native';
import QRScannerView from '../components/QRScannerView/QRScannerView';
import { Colors, Dimens as D } from '../resource';

export default class DefaultScanCodePage extends Component {
  
    

    constructor(props) {
      super(props);
      this.state = {
        focusedScreen: false,
        result: ''
      };
     
    this.requestCameraPermission = this.requestCameraPermission.bind(this)




    }
    componentWillMount(){
        
    }
    
    componentDidMount(){
      const { navigation } = this.props;
     
      // this.didFocusListener = this.props.navigation.addListener('didFocus', () => {
      //     this.setState({ focusedScreen: true });
      //   },
      // );
      this.requestCameraPermission();

    }
    
    componentWillUnmount(){
      // this.didFocusListener && this.didFocusListener.remove();
    }

     //请求权限的方法
     async requestCameraPermission() {
      try {
          const granted = await PermissionsAndroid.request(
              PermissionsAndroid.PERMISSIONS.CAMERA,
              {
                  title: '申请摄像头权限',
                  message:
                      '一个很牛逼的应用想借用你的摄像头，' +
                      '然后你就可以拍出酷炫的皂片啦。',
                  buttonNeutral: '等会再问我',
                  buttonNegative: '不行',
                  buttonPositive: '好吧',
              },
          );
          if (granted === PermissionsAndroid.RESULTS.GRANTED) {

            this.setState({ focusedScreen: true });
              console.log('现在你获得摄像头权限了');
          } else {
              console.log('用户并不屌你');
              this.props.navigation.goBack()
          }
      } catch (err) {
          console.warn(err);
      }
  }

    
    renderTitleBar = () => {
      return (
        // <Text
        //   style={ {
        //     color: 'white',
        //     textAlignVertical: 'center',
        //     textAlign: 'center',
        //     fontSize: D.dp20,
        //     padding: D.dp12,
        //     marginTop: StatusBar.currentHeight,
        //     backgroundColor: Colors.black_00000080,
        //   } }
        // >Scan</Text>
        null
      );
    };
    
    renderMenu = () => {
      
      return (
        // <Text style={ {
        //   color: 'white',
        //   textAlignVertical: 'center',
        //   textAlign: 'center',
        //   fontSize: D.dp20,
        //   padding: D.dp32,
        //   backgroundColor: Colors.black_00000080,
        // } }
        // >Action Menu</Text>
        null
      );
    };
    
    barcodeReceived = (event) => {
    //   Toast.show('Type: ' + event.type + '\nData: ' + event.data);

      var callback = this.props.route.params.callBack
      callback( event.data+"")

      this.props.navigation.pop()


      this.setState({result:  event.data})
    };
    
    render(){
      const { focusedScreen } = this.state;
      
      return (
        <Fragment>
          <StatusBar barStyle='light-content' translucent={ true } backgroundColor={ Colors.transparent }/>
          <SafeAreaView style={ { flex: 1, backgroundColor: Colors.black_0000004D } }>
            {
              focusedScreen
                ? < QRScannerView
                  onScanResult={ this.barcodeReceived }
                  renderHeaderView={ this.renderTitleBar }
                  renderFooterView={ this.renderMenu }
                  scanBarAnimateReverse={ true }
                />
                : null
            }
          </SafeAreaView>
        </Fragment>
      );
    }
  }
  
  